package com.ruoyi.park.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.exception.CustomException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.park.domain.BuiPark;
import com.ruoyi.park.domain.WyLampManagement;
import com.ruoyi.park.domain.vo.WyLampManagementVO;
import com.ruoyi.park.mapper.BuiParkMapper;
import com.ruoyi.park.mapper.WyLampManagementMapper;
import com.ruoyi.park.service.IWyLampManagementService;
import com.ruoyi.system.api.RemoteBmCodeService;
import com.ruoyi.system.api.domain.RemoteSysBmType;
import com.ruoyi.system.api.enums.SysBmCodeTypeValue;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 路灯管理Service业务层处理
 *
 * @author cjie
 * @date 2021-07-22
 */
@Service
public class WyLampManagementServiceImpl implements IWyLampManagementService {
    @Autowired
    private WyLampManagementMapper wyLampManagementMapper;

    @Autowired
    private BuiParkMapper buiParkMapper;

    @Autowired
    private RemoteBmCodeService remoteBmCodeService;

    /**
     * 查询路灯管理
     *
     * @param id 路灯管理ID
     * @return 路灯管理
     */
    @Override
    public WyLampManagement selectWyLampManagementById(String id) {
        return wyLampManagementMapper.selectWyLampManagementById(id);
    }

    /**
     * 查询路灯管理列表
     *
     * @param wyLampManagement 路灯管理
     * @return 路灯管理
     */
    @Override
    public List<WyLampManagement> selectWyLampManagementList(WyLampManagement wyLampManagement) {
        return wyLampManagementMapper.selectWyLampManagementList(wyLampManagement);
    }

    /**
     * 新增路灯管理
     *
     * @param wyLampManagement 路灯管理
     * @return 结果
     */
    @Override
    public int insertWyLampManagement(WyLampManagement wyLampManagement) {
        wyLampManagement.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        wyLampManagement.setCreateTime(DateUtils.getNowDate());
        wyLampManagement.setCreateBy(SecurityUtils.getUsername());

        if(wyLampManagement.getOpenTime() != null && wyLampManagement.getOffTime() != null){
            long duration = DateUtil.between(wyLampManagement.getOpenTime(), wyLampManagement.getOffTime(),
                    DateUnit.SECOND);
            wyLampManagement.setDuration(secondCover(duration));
        }
        return wyLampManagementMapper.insertWyLampManagement(wyLampManagement);
    }

    /**
     * 修改路灯管理
     *
     * @param wyLampManagement 路灯管理
     * @return 结果
     */
    @Override
    public int updateWyLampManagement(WyLampManagement wyLampManagement) {
        wyLampManagement.setUpdateTime(DateUtils.getNowDate());
        wyLampManagement.setUpdateBy(SecurityUtils.getUsername());
        return wyLampManagementMapper.updateWyLampManagement(wyLampManagement);
    }

    /**
     * 批量删除路灯管理
     *
     * @param ids 需要删除的路灯管理ID
     * @return 结果
     */
    @Override
    public int deleteWyLampManagementByIds(String[] ids) {
        return wyLampManagementMapper.deleteWyLampManagementByIds(ids);
    }

    /**
     * 删除路灯管理信息
     *
     * @param id 路灯管理ID
     * @return 结果
     */
    @Override
    public int deleteWyLampManagementById(String id) {
        return wyLampManagementMapper.deleteWyLampManagementById(id);
    }

    @Override
    public String importData(List<WyLampManagementVO> buiRoomVOList) {
        if (StringUtils.isNull(buiRoomVOList) || buiRoomVOList.size() == 0) {
            throw new CustomException("导入抄表数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (WyLampManagementVO buiRoomVO : buiRoomVOList) {
            try {
                this.insertData(buiRoomVO);
                successNum++;
                //successMsg.append("<br/>" + successNum + "、记录 " + buiRoomVO.getRoomNum() + " 导入成功");
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、记录 "  + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    private void insertData(WyLampManagementVO buiRoomVO){
        WyLampManagement buiRoom = new WyLampManagement();
        BeanUtils.copyProperties(buiRoomVO,buiRoom);
        BuiPark buiPark = buiParkMapper.selectBuiParkByParkNum(buiRoomVO.getParkNum());
        if(StringUtils.isNotNull(buiPark)){
            buiRoom.setParkId(buiPark.getId());
        }
        RemoteSysBmType bmType = remoteBmCodeService.getCodeByGenCode(buiRoomVO.getDeviceNum(), SysBmCodeTypeValue.DEVICE);
        buiRoom.setDeviceNum(bmType.getNowCode());

        this.insertWyLampManagement(buiRoom);
    }

    /**
     * 把秒转换成 时：分：秒
     *
     * @param second
     * @return
     */
    public static String secondCover(Long second) {
        Long temp = 0L;
        StringBuffer sb = new StringBuffer();
        temp = second / 3600;
        sb.append((temp < 10) ? "0" + temp + ":" : "" + temp + ":");

        temp = second % 3600 / 60;
        sb.append((temp < 10) ? "0" + temp + ":" : "" + temp + ":");

        temp = second % 3600 % 60;
        sb.append((temp < 10) ? "0" + temp : "" + temp);
        return sb.toString();
    }
}
